/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javaapplication6.UI;

/**
 *
 * @author meydlo
 */


import javaapplication6.ControlLayer.RentControl;
import javaapplication6.DomainLayer.RentProduct;
public class RentUI {

    private RentControl rentControl;
    private Listener listener=new Listener();


    public RentUI(){
        rentControl=new RentControl();
    }
    public void printChoices()
    {
        System.out.println(
                "\n (1) create rentProduct"+
                "\n (2) print rentProduct info"+
                "\n (3) update rentProduct"+
                "\n (4) delete rentProduct"+
                "\n (5) print choices"+
                "\n (6) print rentProductList"+
                "\n (7) main menu"
                );
    }
    public void rentProductManager()
    {
        boolean exit=false;
        while(exit==false){
            printChoices();
            int number=listener.getLineInt();
            switch(number){
                case 1:{
                    createRentProduct();
                    break;
                }
                case 2:{
                    printRentProductInfo();
                    break;
                }
                case 3:{
                    updateRentProduct();
                    break;
                }
                case 4:{
                    deleteRentProduct();
                    break;
                }
                case 5:{
                    printChoices();
                    break;
                }
                case 6:{
                    printRentProductList();
                    break;
                }
                case 7:{
                    exit=true;
                    break;
                }
                case -1:{
                    System.out.println("Please write a number");
                    break;
                }
                default:{
                    System.out.println("Valid rank id: 1-7");
                }
            }
        }
    }

    public void createRentProduct()
    {
        String name=getNameUI();
        RentProduct rentProduct=rentControl.getRentProductByName(name);
        if(rentProduct==null){

            System.out.println("~Creating started~");
           //get info abou product
           int noOfOneType=getnoOfOneTypeUI();
           String description=getDescriptionUI();
           double price=getPriceUI();
           double discount=getDiscountUI();

           //store info to product....-> to arrayList
           rentProduct=rentControl.createRentProduct(name, description, noOfOneType, price,discount);
        }
        else{
            System.out.println("Same product was found!");
            int noToAdd=getnoOfOneTypeUI();
            int noAlreadyStored=rentProduct.getNoOfOneType();

            int together=noAlreadyStored+noToAdd;
            rentProduct.setNoOfOneType(together);
        }

        //print noOfType after added
        System.out.println("Now goods of this type in stok: "+rentProduct.getNoOfOneType());
        System.out.println("product for sale successful added");
    }

    protected RentProduct getRentProductByIDui()
    {

        RentProduct rentProduct;
        do{
            long idOfSaleProduct=getIdOfProductUI();
            rentProduct=rentControl.findRentProduct(idOfSaleProduct);
            if(rentProduct==null){
                System.out.println("RentProduct wasn't found");
                System.out.println("try another id? (y/n):");
                if(listener.getLineBoolean()==false) break;
            }
        }while(rentProduct==null);
        return rentProduct;

    }

    public void printRentProductInfo()
    {
        try{
            RentProduct rentProduct=getRentProductByIDui();
            rentProduct.printProductInfo();
        }
        catch(Exception e){
        }
    }

    public void printRentProductList()
    {
        rentControl.printRentProductList();
    }

    public void updateRentProduct()
    {
        boolean exit=false;
        RentProduct rentProduct=getRentProductByIDui();
        do{
            if(rentProduct==null) break;
            System.out.println("what you need to change?: ");
            printChoiceOfChanges();
            int line=listener.getLineInt();
            switch(line){
                case 1:{
                    String name=getNameUI();
                    rentProduct.setName(name);
                    break;
                }
                case 2:{
                    double price=getPriceUI();
                    rentProduct.setPrice(price);
                    break;
                }
                case 3:{
                    double discount=getDiscountUI();
                    rentProduct.setDiscount(discount);
                    break;
                }
                case 4:{
                    int noOfOneType=getnoOfOneTypeUI();
                    rentProduct.setNoOfOneType(noOfOneType);
                    break;
                }
                case 5:{
                    String description=getDescriptionUI();
                    rentProduct.setDescription(description);
                    break;
                }
                case 6:{
                    exit=true;
                    break;
                }
            }

        }while(exit==false);

    }


    public void printChoiceOfChanges()
    {
        System.out.println("\n(1) name: "+
                "\n(2) price: "+
                "\n(3) discount: "+
                "\n(4) amount of same kind"+
                "\n(5) description"+
                "\n(6) exit"
                );
    }

    public void deleteRentProduct()
    {
        RentProduct rentProduct= getRentProductByIDui();
        if(rentProduct!=null){
            long idOfProduct=rentProduct.getId();
            rentControl.deleteRentProduct(idOfProduct);
        }
    }

    public String getNameUI()
    {
        System.out.println("name: ");
        String name=listener.getLineString();
        return name;
    }

    public double getPriceUI()
    {
        System.out.println("price: ");
        double price=listener.getLineDouble();
        return price;
    }

    public double getDiscountUI()
    {
        System.out.println("discount: ");
        double discount=listener.getLineDouble();
        return discount;
    }

    public int getnoOfOneTypeUI()
    {
        System.out.println("amount of same kind: ");
        int noOfOneType=listener.getLineInt();
        return noOfOneType;
    }

    public String getDescriptionUI()
    {
        System.out.println("description: ");
        String description=listener.getLineString();
        return description;
    }

    public long getIdOfProductUI()
    {
        System.out.println("write ID of the rentProduct: ");
        long idOfProduct=listener.getLineInt();
        return idOfProduct;
    }
}
